package com.dingdang.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dingdang.data.Result;
import com.dingdang.dto.UserDTO;
import com.dingdang.entity.CarIntention;
import com.dingdang.mapper.CarIntentionMapper;
import com.dingdang.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 董书乾
 * @since 2021-12-20
 */
@RestController
@RequestMapping("/carIntention")
@Api("买卖意向表")
public class CarIntentionController {
    @Resource
    private CarIntentionMapper carIntentionMapper;
    @Resource
    private JwtUtil jwtUtil;

    //    买卖车
    @PostMapping
    @ApiOperation(value = "增加买卖车辆")
    @ApiImplicitParam(name = "carIntention", value = "买卖意向表实体类", paramType = "body", required = true)
    public Result addCar(@RequestBody CarIntention carIntention, HttpServletRequest request) {
        String token = request.getHeader("authToken");
        Claims jwt = jwtUtil.parseJwt(token);
        UserDTO dto = JSONObject.parseObject(jwt.getSubject(), UserDTO.class);
        CarIntention intention = JSONObject.parseObject(JSONObject.toJSONString(carIntention), CarIntention.class);
        BeanUtils.copyProperties(dto, intention);
        carIntentionMapper.insert(intention);
        return Result.ok();
    }

    //    买卖订单查询
    @GetMapping
    @ApiOperation(value = "买卖订单查询")
    @ApiImplicitParam(name = "dealType",value = "买卖状态1为卖2为买",paramType = "query",required = true)
    public Result getCar(@RequestParam("dealType") String dealType) {
        QueryWrapper<CarIntention> wrapper = Wrappers.<CarIntention>query().like("deal_type", dealType).and(dispose -> dispose.like("dispose", "0"));
        List<CarIntention> carIntentionList = carIntentionMapper.selectList(wrapper);
        return Result.ok().setData(carIntentionList);
    }


}
